﻿//QuotingToolApp.controller('SearchOpportunityController', ['$scope', '$uibModalInstance', 'CommonServices', function ($scope, $uibModalInstance, CommonServices) {
QuotingToolApp.controller('SearchOpportunityController', ['$scope', '$uibModalInstance', 'CommonServices', 'QuoteSFDCData', function ($scope, $uibModalInstance, CommonServices, QuoteSFDCData) {

    //initalize data
    $scope.selectedOpp = null;
    $scope.showError = false;
    $scope.showWarning = false;
    $scope.message = "";

    CommonServices.getTopDivisions()
               .$promise
               .then(function (results) {
                   $scope.topDivisions = results;
               })
               .catch(function (response) {
                   $scope.showError = true;
                   $scope.message = "Top divisions load failed";
               }
           );   

    $scope.getters = {
        CloseDate: function (row) {
            return new Date(row.CloseDate);
        },        
        Amount: function (row) {
            return parseFloat(row.Amount);
        }
    }



    $scope.getAccounts = function (query) {

        if (query) {

            return CommonServices.getAccounts(query)
                .$promise
                .then(function (results) {
                    return results.slice(0, 15);                 

                })
                .catch(function (response) {
                    console.log(response);                  
                }
            );
        }
        else {
            return null;          
        }
    }
   
    $scope.getEmployees = function (query) {

        if (query) {

            return CommonServices.getEmployees(query)
                .$promise
                .then(function (results) {
                    return results.slice(0, 15);

                })
                .catch(function (response) {
                    console.log(response);
                }
            );
        }
        else {
            return null;
        }
    },

    $scope.search = function (opportunitysearchform) {
        $scope.showWarning = false;
        $scope.message = "";

        if (opportunitysearchform.$valid) {
            //initilize search parameters object
            var searchParams = {
                oppNumber: 'null',
                oppName: 'null',
                oppOwner: 'null',
                accountID: 'null',
                topDivision: 'null'                                    
            };

            if ($scope.OppNumber) searchParams.oppNumber = $scope.OppNumber;
            if ($scope.OppOwner) searchParams.oppOwner = $scope.OppOwner.FullName;
            if ($scope.OppName) searchParams.oppName = $scope.OppName;
            if ($scope.selectedAccount) searchParams.accountID = $scope.selectedAccount.AccountID;
            if ($scope.topDivisions.selectedOption) searchParams.topDivision = $scope.topDivisions.selectedOption.TopDivisionName;

            $scope.searchResults = [];

            QuoteSFDCData.searchSFDC(searchParams)
                   .$promise
                   .then(function (results) {
                       var notFound = false;
                       if (results != null) {
                           if (results.length > 0) {
                               $scope.searchResults = $scope.displaySearchResults =  results;
                               if ($scope.searchResults.length == 1) $scope.setSelectedOpp(0);
                           }
                           else {
                               $scope.showWarning = true;                               
                           }
                       }
                       else {
                           $scope.showWarning = true;                         
                       }

                       if ($scope.showWarning) $scope.message = 'Opportunities not found';
                   })
                   .catch(function (response) {
                       $scope.showError = true;
                       $scope.message = "Error searching opportunities";               
                       console.log(response);
                   }
               );
        }
    };

    $scope.setSelectedOpp = function (oppNumber) {
        var indexes = $.map($scope.displaySearchResults, function(obj, index) {
            if(obj.OppNumber == oppNumber) {
                return index;
            }
        })
        var oppNumberIndex = indexes[0];        
        $scope.selectedOpp = $scope.displaySearchResults[oppNumberIndex].OppNumber; //$scope.searchResults[index].OppNumber;
    };

    $scope.ok = function () {       
        $uibModalInstance.close($scope.selectedOpp);
    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
    
}]);